﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Xml;

namespace MagickServer.Utils {
    public class RoutingConfigSectionHandler : IConfigurationSectionHandler {
        public object Create(object parent, object configContext, XmlNode section) {
            var l = new List<ImageRoute>();
            foreach (XmlNode route in section.SelectNodes("route")) {
                var rx = new Regex(route.SelectSingleNode("url").InnerText);
                var action = route.SelectSingleNode("action").InnerText;
                var param = GetParameters(route.SelectSingleNode("parameters"));
                var r = new ImageRoute(rx, action, param);
                l.Add(r);
            }
            return l;
        }

        public IDictionary<string, string> GetParameters(XmlNode p) {
            var r = new Dictionary<string, string>();
            foreach (XmlNode n in p.ChildNodes)
                r[n.Name] = n.InnerText;
            return r;
        }
    }
}